Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.IO

Namespace C4F.DevKit.Speech.Controls
	''' <summary>
	''' A control for using the SpeechAPI to read text from a textbox or a file.
	''' </summary>
	Public Partial Class TextToSpeech
		Inherits UserControl
		#Region "Private Variables"

		Private _sapiManager As SapiManager = Nothing
		Private _paused As Boolean = False

		Private Const DESIGNER_CATEGORY As String = "Speech"

		#End Region ' Private Variables

		#Region "Constructors"

		''' <summary>
		''' Initializes a new instance of the TextToSpeech control.
		''' </summary>
		Public Sub New()
			InitializeComponent()

			If (Not Me.DesignMode) Then
				InitializeControl()
			End If
		End Sub

		#End Region ' Constructors

		#Region "Public Properties"

		''' <summary>
		''' The text to be set as the text to read.
		''' </summary>
		<Browsable(True), Category(DESIGNER_CATEGORY)> _
		Public Property SpeechText() As String
			Get
				Return Me.tbTextToSpeech.Text
			End Get
			Set(ByVal value As String)
				If value Is Nothing Then
					Me.tbTextToSpeech.Text = String.Empty
				Else
					Me.tbTextToSpeech.Text = value
				End If
			End Set
		End Property

		<Browsable(True), Category(DESIGNER_CATEGORY)> _
		Public Property IsSsml() As Boolean
			Get
				Return chkSsmlText.Checked
			End Get
			Set(ByVal value As Boolean)
				chkSsmlText.Checked = value
			End Set
		End Property

		''' <summary>
		''' The SAPI Manager to be used by this control.
		''' </summary>
		<Browsable(True), Category(DESIGNER_CATEGORY)> _
		Public Property SapiManager() As SapiManager
			Get
				Return _sapiManager
			End Get
			Set(ByVal value As SapiManager)
				_sapiManager = value

				If Not _sapiManager Is Nothing Then
					AddHandler _sapiManager.StateChanged, AddressOf sapi_StateChanged
				End If
			End Set
		End Property

		#End Region ' Public Properties

		#Region "Event Handlers"

		Private Sub btnToWav_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnToWav.Click
			Try
				_sapiManager.ToWave(tbTextToSpeech.Text, chkSsmlText.Checked)
			Catch
				MessageBox.Show("Error occurred while converting to Wave file.!!", "SpeechGUI", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub tbTextToSpeech_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbTextToSpeech.TextChanged
			OnTextChanged()
		End Sub

		Private Sub btnStop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStop.Click
			Try
				OnStopSpeaking()

			Catch ex As Exception
				DisplayError("The operation could not be stopped.", ex)
			End Try
		End Sub

		Private Sub btnPause_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPause.Click
			Try
				OnPause()
			Catch ex As Exception
				DisplayError("The operation could not be paused.", ex)
			End Try
		End Sub

		Private Sub btnSpeak_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSpeak.Click
			Try
				OnSpeak()
			Catch ex As Exception
				DisplayError("The speak operation could not be started.", ex)
			End Try
		End Sub

		Private Sub btnResume_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnResume.Click
			Try
				OnResumeSpeaking()
			Catch ex As Exception
				DisplayError("The operation could not be resumed.", ex)
			End Try
		End Sub
		Private Sub btnLoadFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoadFile.Click
			Try
				OnLoadFile()
			Catch ex As Exception
				DisplayError("File could not be loaded.", ex)
			End Try
		End Sub

		''' <summary>
		''' Called when the state of the SapiManager has changed.
		''' </summary>
		Private Sub sapi_StateChanged()
			ChangeButtonState()
		End Sub

		#End Region ' Event Handlers

		#Region "Private"

		''' <summary>
		''' Initializes the control for use.
		''' </summary>
		Private Sub InitializeControl()
			Try
				_paused = False
				Me.btnToWav.Enabled = False
				Me.btnSpeak.Enabled = False
				Me.btnPause.Enabled = False
				Me.btnResume.Enabled = False
				Me.btnStop.Enabled = False
			Catch ex As Exception
				MessageBox.Show(ex.Message, "SpeechGUI", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		''' <summary>
		''' Updates the UI to change the state of certain buttons.
		''' </summary>
		Private Sub ChangeButtonState()
			Select Case _sapiManager.State
				Case SynthesizerState.Paused
					Me.btnSpeak.Enabled = False
					Me.btnPause.Enabled = False
					Me.btnResume.Enabled = True
					Me.btnToWav.Enabled = False
					Me.btnStop.Enabled = True
					Me.tbTextToSpeech.Enabled = False
					Me.chkSsmlText.Enabled = False
				Case SynthesizerState.Ready
					Me.btnSpeak.Enabled = True
					Me.btnPause.Enabled = False
					Me.btnResume.Enabled = False
					Me.btnToWav.Enabled = True
					Me.btnStop.Enabled = False
					Me.tbTextToSpeech.Enabled = True
					Me.chkSsmlText.Enabled = True
				Case SynthesizerState.Speaking
					Me.btnSpeak.Enabled = False
					Me.btnPause.Enabled = True
					Me.btnResume.Enabled = False
					Me.btnToWav.Enabled = False
					Me.btnStop.Enabled = True
					Me.tbTextToSpeech.Enabled = False
					Me.chkSsmlText.Enabled = False
			End Select
		End Sub

		''' <summary>
		''' Displays the exception error.
		''' </summary>
		''' <param name="message">The message to be shown.</param>
		''' <param name="ex">The exception to be displayed along with the error message.</param>
		Private Sub DisplayError(ByVal message As String, ByVal ex As Exception)
			If message Is Nothing Then
				message = String.Empty
			End If

			Dim exMessage As String
			If ex Is Nothing Then
				exMessage = String.Empty
			Else
				exMessage = String.Concat(Constants.vbLf + Constants.vbLf, ex.ToString())
			End If
			MessageBox.Show(String.Format("{0}{1}", message, ex.ToString()), "TextToSpeech", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Sub

		Private Sub OnLoadFile()
			tbTextToSpeech.Text = _sapiManager.GetTextFromFile()
		End Sub

		''' <summary>
		''' Pauses the speak operation
		''' </summary>
		Private Sub OnPause()
			If (Not _paused) Then
				_sapiManager.PauseSpeaking()
				_paused = True
			End If
		End Sub

		''' <summary>
		''' Resumes the speak operation
		''' </summary>
		Private Sub OnResumeSpeaking()
			If _paused Then
				_sapiManager.ResumeSpeaking()
				_paused = False
			End If
		End Sub

		''' <summary>
		''' Starts the speak operation
		''' </summary>
		Private Sub OnSpeak()
			If (Not chkSsmlText.Checked) Then
				_sapiManager.Speak(tbTextToSpeech.Text)
			Else
				_sapiManager.SpeakSsml(tbTextToSpeech.Text)
			End If
		End Sub

		''' <summary>
		''' Stops the speak operation
		''' </summary>
		Private Sub OnStopSpeaking()
			_sapiManager.StopSpeaking()
		End Sub

		''' <summary>
		''' Updates UI when the text is changed.
		''' </summary>
		Private Overloads Sub OnTextChanged()
			btnSpeak.Enabled = tbTextToSpeech.Text <> String.Empty
			btnToWav.Enabled = btnSpeak.Enabled
		End Sub

		#End Region ' Private       
	End Class
End Namespace